package com.dongbao.window;

import com.dongbao.module.ScanResult;
import com.dongbao.utils.IdeaApiUtil;
import com.intellij.openapi.diagnostic.Logger;
import com.intellij.openapi.fileEditor.FileEditor;
import com.intellij.openapi.fileEditor.FileEditorManager;
import com.intellij.openapi.project.Project;
import com.intellij.openapi.ui.SimpleToolWindowPanel;
import com.intellij.openapi.wm.ToolWindow;

import javax.swing.*;
import javax.swing.event.HyperlinkEvent;
import javax.swing.event.HyperlinkListener;
import javax.swing.table.DefaultTableModel;
import javax.swing.table.TableModel;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.util.List;

public class DHGLangWindow extends SimpleToolWindowPanel implements HyperlinkListener {

    private static final Logger LOGGER = Logger.getInstance(DHGLangWindow.class);

    private final Object[] columnNames = {"序号", "模块", "包名", "类名", "类型", "内容"};

    private List<ScanResult> results;

    private JPanel contentPlan;
    private JTabbedPane tabbedPane1;
    private JTable langsList;
    private JButton exeuBatch;
    private JTable exeuListTable;

    private Project myProject;

    private Object[][] langsListRowData;

    public DHGLangWindow(Project project, ToolWindow toolWindow) {
        super(true, false);
        this.setContent(contentPlan);
        this.myProject = project;
        //初始化面板
        initPlan();
    }

    private void initPlan() {
        Object[][] rowData = {};
        TableModel dataModel = new DefaultTableModel(rowData, columnNames);
        langsList.setModel(dataModel);

        langsList.addMouseListener(new MouseAdapter() {
            @Override
            public void mouseClicked(MouseEvent e) {
                if (e.getClickCount() == 1) {
                    //实现双击 {
                    int row = ((JTable) e.getSource()).rowAtPoint(e.getPoint()); //获得行位置
                    int col = ((JTable) e.getSource()).columnAtPoint(e.getPoint());
                    //获得列位置
                    TableModel tbModel = langsList.getModel();
                    Object cellVal = (String) (tbModel.getValueAt(row, col));
//                     获得点击单元格数据
                    ScanResult scanResult = results.get(row);
//                    PluginModueExamInfo modueExamInfo = results.get(row);
                    FileEditorManager fileEditorManager = FileEditorManager.getInstance(myProject);
                    FileEditor[] fileEditors = null;
                    if (null != scanResult.getPsiClass()) {
                        fileEditors = fileEditorManager.openFile(scanResult.getPsiClass().getContainingFile().getVirtualFile(), true);
                    } else {
                        fileEditors = fileEditorManager.openFile(scanResult.getVirtualFile(), true);
                    }
                    Integer lineNumber = scanResult.getLineNumber();
                    IdeaApiUtil.gotoLine(lineNumber + 1, myProject);
                }
            }
        });

        //给执行批量替换按钮添加点击监听事件
        exeuBatch.addActionListener((actionEvent -> {
            //执行自动处理,批量替换
            exeuBatchReplace();
        }));
    }

    /**
     * 开始执行多语言占位符替换
     * */
    private void exeuBatchReplace() {
        Object[] exeuListTableColumnNames = new Object[columnNames.length + 1];
        for(int i=0;i<columnNames.length;i++){
            exeuListTableColumnNames[i] = columnNames[i];
        }
        exeuListTableColumnNames[columnNames.length] = "执行状态";

        TableModel dataModel = new DefaultTableModel(this.langsListRowData, columnNames);
        exeuListTable.setModel(dataModel);
    }

    public void referTableData(Object[][] rowData, List<ScanResult> results) {
        langsList.clearSelection();
        this.langsListRowData = rowData;
        TableModel dataModel = new DefaultTableModel(rowData, columnNames);
        langsList.setModel(dataModel);
        this.results = results;
    }

    public DHGLangWindow(boolean vertical) {
        super(vertical);
    }

    public DHGLangWindow(boolean vertical, boolean borderless) {
        super(vertical, borderless);
    }

    /**
     * 处理HTML中的点击超链接事件
     */
    @Override
    public void hyperlinkUpdate(HyperlinkEvent e) {

    }
}
